Page({

  /**
   * 页面的初始数据
   */
  data: {
      morningnum:'0',
      afternoonnum:"1",
      doctorname:'',
      personname:'',
      sex:'',
      age:'',
      times:'',
      unitphone:'',
      doctorId:'',
      productInfo:{},
      img_arr: [],  
      sickSketch:'',
      morningColor:'rgba(218, 207, 245, 0.884)',
      afternoonColor:'rgba(218, 207, 245, 0.884)',
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      sickSketch:getApp().globalData.sick,
    });
  this.setData({
    doctorId: getApp().globalData.doctorId,
  })
  this.setData({
    unitphone: getApp().globalData.unitphone,
  })
    var that=this
      wx.request({
        url:  getApp().globalData.url+'showOrderInfo', // 仅为示例，并非真实的接口地址
        method: 'GET',
        data: {
          bookdate: getApp().globalData.bookdate,
          unitphone:getApp().globalData.unitphone,
          doctorId:getApp().globalData.doctorId
        },

        header: {
          'content-type': 'application/x-www-form-urlencoded' // 默认值
        },
        success(res) {
          console.log(res.data)
            that.setData({
              doctorname:res.data.doctorName
            })
            that.setData({
              personname: res.data.patientName
            })
            that.setData({
              sex: res.data.patientSex
            })
            that.setData({
              age: res.data.patientAge
            })
            that.setData({
              morningnum: res.data.morningNum
            })
            if(that.data.morningnum==0)
            {
              that.setData({
                morningColor:'rgb(87, 84, 84)',
               })
            }
            that.setData({
              afternoonnum: res.data.afternoonNum
            })
            if(that.data.afternoonnum==0)
            {
              that.setData({
                afternoonColor:'rgb(87, 84, 84)',
               })
            }
            console.log(res.data)
            console.log(res.data.patientName)
            console.log(that.data)
        },fail(res) {
             wx.showToast({
             title: '网络连接失败，请重新尝试',
            icon:'none'
           })
           if(that.data.morningnum==0)
            {
              that.setData({
                morningColor:'rgb(126, 117, 117)',
               })
            }
        }
      })
  },
  deleteImage:function(){
  var that=this;
  wx.showModal({
    title: '提示',
    content: '确定要删除此图片吗？',
    success: function (res) {
     if (res.confirm) 
     {
     that.data.img_arr.splice(0, 1);
     } 
     else if (res.cancel) 
     { 
     }
      that.setData({
        img_arr:that.data.img_arr
      })
    }
  })
  },
morning:function(){
  this.setData({
    doctorId: getApp().globalData.doctorId,
  })
  this.setData({
    unitphone: getApp().globalData.unitphone,
  })
  if(this.data.morningnum==0)
  {
    wx.showToast({
      title: '该时段预约已满',
      icon: 'none',
    })
  }
  else
  {
    if(this.data.afternoonnum!=0)
    {
      this.setData({
        afternoonColor:'rgba(218, 207, 245, 0.884)',
      })
    }
    if(this.data.morningColor=='rgba(252,178,22, 0.3)')
    {
      this.setData({
        morningColor:'null',
        times:'',
      })
      console.log(this.data.times)
    } 
     else{
      this.setData({
        morningColor:'rgba(252,178,22, 0.3)',
        times:'上午'
      })
      wx.showToast({
        title: '已选中上午',
        icon: 'none',
      })
      console.log(this.data.times)
    }
  }
},
afternoon:function(){
  this.setData({
    doctorId: getApp().globalData.doctorId,
  })
  this.setData({
    unitphone: getApp().globalData.unitphone,
  })
  if(this.data.afternoonnum==0)
  {
    wx.showToast({
      title: '该时段预约已满',
      icon: 'none',
    })
  }
  else
  {
   
    if(this.data.morningnum!=0)
    {
      this.setData({
        morningColor:'rgba(218, 207, 245, 0.884)',
      })
    }
    if(this.data.afternoonColor=='rgba(252,178,22, 0.3)')
    {
      this.setData({
        afternoonColor:'null',
        times:'',
      })
      console.log(this.data.times)
    } 
     else{
      this.setData({
        afternoonColor:'rgba(252,178,22, 0.3)',
        times:'下午'
      })
      wx.showToast({
        title: '已选中下午',
        icon: 'none',
      })
      console.log(this.data.times)
    }
  }
},   
uploadImage:function(){
var that=this;
wx.chooseImage({
  count: 1,//最多可以上传图片数
  sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图，默认二者都有 
  sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机，默认二者都有 
  success:function(res){
    that.setData({ 
      img_arr:res.tempFilePaths[0] 
    }) 
  }
})
},                            
update:function(){
  console.log(this.data.sickSketch)
  if(this.data.times=="")
    {
      wx.showToast({
        title: '请选择预约时间',
        icon: 'none',
      })
    }
    else if(this.data.sickSketch=="")
    {
      wx.showToast({
        title: '请描述病情',
        icon: 'none',
      })
    }
    else if(this.data.img_arr.length==0)
    {
      wx.showToast({
        title: '请上传病情图片',
        icon: 'none',
      })
    }
    else if(this.data.img_arr.length>1)
    {
      wx.showToast({
        title: '只能上传一张图',
        icon: 'none',
      })
    }
    else{
      console.log(this.data.times)
      console.log(this.data.sickSketch)
       var that = this;
       
      wx.showModal({
        title: '提示',
        content: '您确定要上传吗?',
        success: function (res) {
          if(res.confirm){
          var app=getApp();
          for (var i=0; i< that.data.img_arr.length; i++) 
          {  
            wx.uploadFile({  
              url:  getApp().globalData.url+'patientSelectOrderTime',
              filePath: that.data.img_arr[i],  
              name: 'file',  
              formData: {
                doctorId: getApp().globalData.doctorId,
                unitphone:getApp().globalData.unitphone,
                bookdate: getApp().globalData.bookdate,
                bootTime:that.data.times,
                sickSketch:that.data.sickSketch,
              },  
              header: {
              'content-type': 'application/x-www-form-urlencoded' // 默认值
              },
            success: function (res) {  
              console.log(that.data.times)
              console.log(res.data.success)
              console.log(res.data)
              if(res.data=="True")
              {
                wx.showToast({
                  //title: res.data.messege+res.data.bookId,
                  title:"预约成功",
                  icon:'none'
                })
                wx.navigateTo({
                  url:'../success/success',
                })
              }
              else{
                wx.showToast({
                   title: "预约失败",
                    icon:'none'
                 })
              }
            } ,fail(res) {
               console.log(that.data.sickSketch)
                wx.showToast({
                  title: '网络连接失败，请重新尝试',
                  icon:'none'
                 })
                
             }
            })
          }
          }
        else {//这里是点击了取消以后
        }
      }
    })
  }
},

  inputill:function(e){
  this.setData({
    sickSketch:e.detail.value
  });
  getApp().globalData.sick=this.data.sickSketch;
  console.log(getApp().globalData.sick)
  },
  upimg: function () 
  {  
    var that = this; 
    var length=this.data.img_arr.length; 
    console.log(length);
    if (this.data.img_arr.length<1){  
    wx.chooseImage({  
      sizeType: ['original', 'compressed'],  
      success: function (res) {  
        that.setData({  
          img_arr: that.data.img_arr.concat(res.tempFilePaths),

        })  

      }  
    })  
    }else{  
      wx.showToast({  
        title: '只能上传一张图',  
        duration: 300 
      });  
    } 
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      sickSketch:getApp().globalData.sick,
    });
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})